<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="日期">
                <a-range-picker :allowClear="true" ref="dateRangePicker" @change="dateRangePickerChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="代理名称">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="代理手机号">
                <a-input v-model="queryParam.phone" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button
                  type="primary"
                  @click="refreshList"
                >查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                <a-button style="margin-left: 8px" @click="handleExport">导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-tabs default-active-key="1">
        <a-tab-pane key="1" tab="待审核">
          <s-table
            ref="pendingTable"
            size="default"
            rowKey="id"
            :columns="pendingColumns"
            :alert="pendingOptions.alert"
            :rowSelection="pendingOptions.rowSelection"
            :data="loadPendingData"
            showPagination="auto"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="handleApprove([record.id], 2)">同意打款</a>
              <a-divider type="vertical"/>
              <a @click="handleApprove([record.id], 3)">拒绝打款</a>
            </span>

            <span slot="footer">
              <a-row type="flex" justify="start">
                <a-col>
                  <a-button
                    @click="handleApprove(pendingSelectedRowKeys, 2)"
                    :disabled="pendingSelectedRowKeys.length === 0"
                    type="primary"
                  >批量同意</a-button>
                </a-col>
                <a-col offset="1">
                  <a-button
                    @click="handleApprove(pendingSelectedRowKeys, 3)"
                    :disabled="pendingSelectedRowKeys.length === 0"
                    type="primary"
                  >批量拒绝</a-button>
                </a-col>
              </a-row>
            </span>
          </s-table>
        </a-tab-pane>
        <a-tab-pane key="2" tab="提现成功" force-render>
          <s-table
            ref="passedTable"
            size="default"
            rowKey="id"
            :columns="passedColumns"
            :data="loadPassedData"
            showPagination="auto"
          />
        </a-tab-pane>
        <a-tab-pane key="3" tab="提现失败" force-render>
          <s-table
            ref="didNotPassedTable"
            size="default"
            rowKey="id"
            :columns="didNotPassedColumns"
            :data="loadDidNotPassedData"
            showPagination="auto"
          />
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script>
  import { STable } from '@/components'
  import PartnersForm from '@/views/proxy/partner/Form'
  import { approveWithdrawals, getWithdrawalsRecordList } from '@/api/finance'
  import { formatAmount } from '@/utils/util'

  export default {
    name: 'List',
    components: {
      PartnersForm,
      STable
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 待审核表头
        pendingColumns: [
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '代理名称',
            dataIndex: 'agent.name'
          },
          {
            title: '身份证',
            dataIndex: 'cardholder_id_number'
          },
          {
            title: '银行卡名称',
            dataIndex: 'bank_name'
          },
          {
            title: '持卡人姓名',
            dataIndex: 'cardholder_name'
          },
          {
            title: '银行卡号',
            dataIndex: 'bank_card_account'
          },
          {
            title: '提现金额',
            dataIndex: 'withdrawal_amount',
            needTotal: true,
            customRender: withdrawalAmount => formatAmount(withdrawalAmount)
          },
          {
            title: '申请时间',
            dataIndex: 'created_at'
          },
          {
            title: '操作',
            width: '150px',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 待审核 选中 配置
        pendingOptions: {
          alert: {
            show: true,
            clear: () => {
              this.pendingSelectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.pendingSelectedRowKeys,
            onChange: this.onSelectedRowKeysChange
          }
        },
        pendingSelectedRowKeys: [],
        // 通过的表头
        passedColumns: [
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '代理名称',
            dataIndex: 'agent.name'
          },
          {
            title: '身份证',
            dataIndex: 'cardholder_id_number'
          },
          {
            title: '银行卡名称',
            dataIndex: 'bank_name'
          },
          {
            title: '持卡人姓名',
            dataIndex: 'cardholder_name'
          },
          {
            title: '银行卡号',
            dataIndex: 'bank_card_account'
          },
          {
            title: '提现金额',
            dataIndex: 'withdrawal_amount',
            needTotal: true,
            customRender: withdrawalAmount => formatAmount(withdrawalAmount)
          },
          {
            title: '申请时间',
            dataIndex: 'created_at'
          },
          {
            title: '备注',
            dataIndex: 'audit_remarks'
          }
        ],
        // 未通过的表头
        didNotPassedColumns: [
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '代理名称',
            dataIndex: 'agent.name'
          },
          {
            title: '身份证',
            dataIndex: 'cardholder_id_number'
          },
          {
            title: '银行卡名称',
            dataIndex: 'bank_name'
          },
          {
            title: '持卡人姓名',
            dataIndex: 'cardholder_name'
          },
          {
            title: '银行卡号',
            dataIndex: 'bank_card_account'
          },
          {
            title: '提现金额',
            dataIndex: 'withdrawal_amount',
            needTotal: true,
            customRender: withdrawalAmount => formatAmount(withdrawalAmount)
          },
          {
            title: '申请时间',
            dataIndex: 'created_at'
          },
          {
            title: '备注',
            dataIndex: 'audit_remarks'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadPendingData: parameter => {
          parameter.status = 1
          return getWithdrawalsRecordList(Object.assign(parameter, this.queryParam))
        },
        loadPassedData: parameter => {
          parameter.status = 2
          return getWithdrawalsRecordList(Object.assign(parameter, this.queryParam))
        },
        loadDidNotPassedData: parameter => {
          parameter.status = 3
          return getWithdrawalsRecordList(Object.assign(parameter, this.queryParam))
        }
      }
    },
    methods: {
      handleReset () {
        this.queryParam = {}
        this.$refs.dateRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      },
      dateRangePickerChange (moment, date) {
        this.queryParam.created_at_begin = date[0]
        this.queryParam.created_at_end = date[1]
      },
      handleApprove (recordIds, status) {
        const _this = this
        const title = status === 2 ? '确认已经打款给该代理了吗？' : '确认拒绝提现申请吗？'

        this.$confirm({
          centered: true,
          keyboard: false,
          title: title,
          content: h => (<a-textarea placeholder = "请填写备注信息，选填..." id = "remark" > < /a-textarea>),
          onOk () {
            return new Promise((resolve, reject) => {
              const remark = document.getElementById('remark').value
              approveWithdrawals({
                remark: remark,
                ids: recordIds,
                status: status
              })
                .then(res => {
                  _this.$message.success('操作成功!')
                  resolve()
                })
                .catch(e => reject(e))
                .finally(() => _this.refreshList())
            })
          },
          onCancel () {
          }
        })
      },
      onSelectedRowKeysChange (pendingSelectedRowKeys) {
        this.pendingSelectedRowKeys = pendingSelectedRowKeys
      },
      refreshList () {
        this.pendingSelectedRowKeys = []
        this.$refs.pendingTable.refresh(true)
        this.$refs.passedTable.refresh(true)
        this.$refs.didNotPassedTable.refresh(true)
      },
      handleExport () {
        getWithdrawalsRecordList(Object.assign({ export: true }, this.queryParam))
      }
    }
  }
</script>
